import numpy as np
import pandas as pd
from sklearn import metrics
from sklearn.metrics import roc_auc_score
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt
import pickle
from time import time

begin = time()

# Functions ------------------------------------------------------------------------------------------------------------
def loadRLosses(savePath_LL, savePath_RL):
    LL = np.load(savePath_LL)
    RL = np.load(savePath_RL)
    return LL, RL

def computeRScores(RL):
    # RL =  (-1) * RL
    drScores = []
    dr_score = RL
    drScores.append(dr_score)
    return drScores

def computeAucRocs(LL, drScores):
    aucRocMax = 0
    aucRocMaxIndex = 0
    index = 0
    for dr_score in drScores:
        aucRoc = roc_auc_score(LL, dr_score)
        print("aucRoc: ", aucRoc)
        if(aucRocMax < aucRoc):
            aucRocMax = aucRoc
            aucRocMaxIndex = index
        index = index + 1
    print("aucRocMaxIndex: ", aucRocMaxIndex)
    print("aucRocMax: ", aucRocMax)
    return aucRocMaxIndex, aucRocMax

def plotAucRoc(LL, drScores, aucRocMaxIndex, savePathPlotAucRoc):
    fpr, tpr, thresholds = metrics.roc_curve(LL, drScores[aucRocMaxIndex], pos_label=1)
    plt.figure(1)
    plt.plot([0, 1], [0, 1], 'k--')
    plt.plot(fpr, tpr, label='ROC Curve')
    plt.xlabel('False positive rate')
    plt.ylabel('True positive rate')
    plt.title('ROC curve')
    plt.legend(loc='best')
    plt.show()
    plt.savefig(savePathPlotAucRoc)

#-----------------------------------------------------------------------------------------------------------------------

# Main Executtion ------------------------------------------------------------------------------------------------------
if __name__ == "__main__":
    print('Main Starting...')
    
    aucRocMaxEpochsGAN = 0
    aucRocMaxEpochsGANIndex = 0
    savePathPart1 = "./Experiments_Autoencoder2/Generator_Only/R_Losses_EpochsAutoencoder/R_Losses_"
    for epoch_Autoencoder in range(0, 800):
        print("\nepoch_Autoencoder: ", epoch_Autoencoder)
        savePath_LL = savePathPart1 + str(epoch_Autoencoder) + "/LL" + ".npy"
        savePath_RL = savePathPart1 + str(epoch_Autoencoder) + "/RL" + ".npy"
        LL, RL = loadRLosses(savePath_LL, savePath_RL)
        drScores = computeRScores(RL)
        aucRocMaxIndex, aucRocMax = computeAucRocs(LL, drScores)
        if(aucRocMaxEpochsGAN < aucRocMax):
            aucRocMaxEpochsGAN = aucRocMax
            aucRocMaxEpochsGANIndex = epoch_Autoencoder

    print("\n\naucRocMaxEpochsGAN: ", aucRocMaxEpochsGAN)
    print("aucRocMaxEpochsGANIndex: ", aucRocMaxEpochsGANIndex)

    # savePathPlotAucRoc = "./Experiments_Autoencoder/Autoencoder_Loss_G/D_R_Losses_EpochsGAN/Auc_ROC_Plots/ROC_Curve.png"
    # plotAucRoc(LL, drScores, aucRocMaxIndex, savePathPlotAucRoc)

    print('Main Terminating...')
    end = time() - begin
    print('Testing terminated | Execution time=%d s' % (end))
#-----------------------------------------------------------------------------------------------------------------------

